<?php
	session_start();
	/**
	* @package admin
	* @desc This php file is used to new add parks to the database
	*/
	/**
	*@desc Function to delete park from the database
	*@param int $park_id
	*/
	function deletepark($park_id)
	{
		global $connection;
		
		//prepare queries
		$deletepark = "DELETE FROM parks WHERE park_id = $park_id";
		$deletecomments = "DELETE FROM comments WHERE park_id = $park_id";
		$deleteequipment = "DELETE FROM parkequipment WHERE park_id =". $park_id;
		$deleteratings = "DELETE FROM ratings WHERE park_id =". $park_id;
		$deletereports = "DELETE FROM reports WHERE park_id =". $park_id;
		
		//run queries
		if (!$result = mysql_query ($deletecomments, $connection))
			die("Failed to delete park comments");
		if (!$result = mysql_query ($deleteequipment, $connection))
			die("Failed to delete park equipment");
		if (!$result = mysql_query ($deleteratings, $connection))
			die("Failed to delete park ratings");
		if (!$result = mysql_query ($deletereports, $connection))
			die("Failed to delete park reports");
		if (!$result = mysql_query ($deletepark, $connection))
			die("Failed to delete PARK");
	}
	
	// Direct calling check
	if ($_SESSION["role"] != "admin")
	{
		header('Location: ../index.php');
		exit();
	}
	if (!isset($_POST["delete"]))
	{
		die("Do not call this page directly");
		exit();
	}
	
	// Get variable
	$park_id = $_POST["park_id"];
	
	if($_POST['delete'] == "Cancel")
	{
		$name = $_SESSION['parkresults']['name'];
		$type = $_SESSION['parkresults']['type'];
		$postcode = $_SESSION['parkresults']['postcode'];
		header("Location: ../parkdetails.php?id=$park_id");
		exit();
	}
	else
	{

		/**
		* @desc This include file connects to the database.
		*/
		require '../includes/connect.inc';
	
		//check if park exists
		$park = "SELECT park_id FROM parks WHERE park_id = $park_id";
	
		//run query
		if (!$result = mysql_query ($park, $connection))
			die("Could not get park id");
		if(mysql_num_rows($result) < 1)
		{
			header('Location: find.php');
			exit();
		}
			
	
		deletepark($park_id);
	
		mysql_close($connection); // Disconnect
	
		header("Location: parkdeleted.php");
		exit();
	}
	
?>